<?php
	/**
	 * @package AmpleHarvest
	 */
	/**
	 * @brief This Class is used to Calculate The next time a task will be run based on Cron
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 24 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes
	 */
	class cScheduler
	{
		private $m_str_Min = "05";
		private $m_str_Hour = "*";
		private $m_str_Day = "*";
		private $m_str_DayOfWeek = "*";
		private $m_str_Month = "*";
		private $m_str_Year = "*";

		/**
		 * This is the constructor for the class
		 * @param string $str_Hour Value that represents the hour it should be run on
		 * @param string $str_Day Value that represents the day it should be run on
		 * @param string $str_DayOfWeek Value that represents the day of the week it should be run on
		 * @param string $str_Month Value that represents the month it should be run on
		 * @param string $str_Year Value that represents the year it should be run on
		 */
		function __construct($str_Hour = "*", $str_Day = "*", $str_DayOfWeek = "*", $str_Month = "*", $str_Year = "*")
		{
			$this->set_Hour($str_Hour);
			$this->set_Day($str_Day);
			$this->set_DayOfWeek($str_DayOfWeek);
			$this->set_Month($str_Month);
			$this->set_Year($str_Year);
		}

		/**
		 * @{
		 * @name Getters
		 * @brief Getter Functions
		 */
		/**
		 * Gets the value min is set to
		 * @return string value for minute
		 */
		public function get_Min()
		{
			return $this->m_str_Min;
		}

		/**
		 * Get the value used to determin Hours
		 * @return string value used to determin hours
		 */
		public function get_Hour()
		{
			return $this->m_str_Hour ;
		}

		/**
		 * Get the value used to determin Day
		 * @return string value used to determin Day
		 */
		public function get_Day()
		{
			return $this->m_str_Day ;
		}

		/**
		 * Get the value used to determin Day Of Week
		 * @return string value used to determin Day of Week
		 */
		public function get_DayOfWeek()
		{
			return $this->m_str_DayOfWeek ;
		}

		/**
		 * Get the value used to determin Month
		 * @return string value used to determin Month
		 */
		public function get_Month()
		{
			return $this->m_str_Month ;
		}

		/**
		 * Get the value used to determin Year
		 * @return string value used to determin Year
		 */
		public function get_Year()
		{
			return $this->m_str_Year ;
		}

		/**
		 * Returns the next date / time this event will happen
		 * @param DateTime $obj_Date This is the date to calculate off of (if blank it will be current time)
		 */
		public function get_Next(DateTime $obj_Date = null)
		{
			if (!$obj_Date)
			{
				$obj_Date = new DateTime();
			}

			//print("Start Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");

			if ($this->get_Min() != "*")
			{
				$int_Temp = $obj_Date->format("i");
				$int_Temp2 = $this->get_Min();
				if ($int_Temp > $int_Temp2)
				{
					$obj_Date->setTime(($obj_Date->format("H") + 1), $int_Temp2);
				}
				else
				{
					$obj_Date->setTime(($obj_Date->format("H")), $int_Temp2);
				}
			}

			//print("After Min Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");

			if ($this->get_Hour() != "*")
			{
				$int_Temp = $obj_Date->format("H");
				$int_Temp2 = $this->get_Hour();
				if ($int_Temp > $int_Temp2)
				{
					$obj_Date->setDate($obj_Date->format("Y"), $obj_Date->format("m"), $obj_Date->format("d") + 1);
					$obj_Date->setTime($int_Temp2, $obj_Date->format("i"));
				}
				else
				{
					$obj_Date->setTime($int_Temp2, $obj_Date->format("i"));
				}
			}

			//print("After Hour Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");

			if ($this->get_Day() != "*")
			{
				$int_Temp = $obj_Date->format("d");
				$int_Temp2 = $this->get_Day();
				if ($int_Temp > $int_Temp2)
				{
					$obj_Date->setDate($obj_Date->format("Y"), $obj_Date->format("m") + 1, $int_Temp2);
				}
				else
				{
					$obj_Date->setDate($obj_Date->format("Y"), $obj_Date->format("m"), $int_Temp2);
				}
			}

			//print("After Day Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");

			if ($this->get_DayOfWeek() != "*")
			{
				$int_Temp = $obj_Date->format("w");
				$int_Temp2 = $this->get_DayOfWeek();
				if ($int_Temp > $int_Temp2)
				{
					$int_i = ($int_Temp2 + 7) - $int_Temp;
				}
				else
				{
					$int_i = ($int_Temp2) - $int_Temp;
				}

				$obj_Date->add(new DateInterval("P" . $int_i . "D"));
			}

			//print("After DayOfWeek Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");

			if ($this->get_Month() != "*")
			{
				$int_Temp = $obj_Date->format("m");
				$int_Temp2 = $this->get_Month();
				if ($int_Temp > $int_Temp2)
				{
					$obj_Date->setDate($obj_Date->format("Y") + 1, $int_Temp2, $obj_Date->format("d"));
				}
				else
				{
					$obj_Date->setDate($obj_Date->format("Y"), $int_Temp2, $obj_Date->format("d"));
				}
			}
			//print("After Month Date - " . $obj_Date->format("w/d/M/Y H:i:s") . "<br/>\n");
			return $obj_Date;
		}
		/**
		 * @}
		 */
		/**
		 * @{
		 * @name Setters
		 * @brief Setter Functions
		 */

		/**
		 * Sets the value for Min
		 * @param string $str_Min
		 */
		public function set_Min($str_Min)
		{
			$this->m_str_Min = $str_Min;
		}

		/**
		 * Sets the value for the Hour
		 * @param string $str_Hour Value used to determin what hours the task is run on
		 */
		public function set_Hour($str_Hour)
		{
			$this->m_str_Hour = $str_Hour;
		}

		/**
		 * Sets the value for the day
		 * @param string $str_Day Value used to determin what day the task is run on
		 */
		public function set_Day($str_Day)
		{
			$this->m_str_Day = $str_Day;
		}

		/**
		 * Sets the value for the day of the week
		 * @param string $str_DayOfWeek Value used to determin what day of the week the task is run on
		 */
		public function set_DayOfWeek($str_DayOfWeek)
		{
			$this->m_str_DayOfWeek = $str_DayOfWeek;
		}

		/**
		 * Sets the value for the Month
		 * @param string $str_Month Value used to determin what Month the task is run on
		 */
		public function set_Month($str_Month)
		{
			$this->m_str_Month = $str_Month;
		}

		/**
		 * Sets the value for the Year
		 * @param string $str_Year Value used to determin what Year the task is run on
		 */
		public function set_Year($str_Year)
		{
			$this->m_str_Year = $str_Year;
		}

		/**
		 * @}
		 */
	}
